﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Hangjing.Common;
using System.Text.RegularExpressions;
using Hangjing.SQLServerDAL;
using Hangjing.Model;

public partial class Ajax_SendGmsCode : System.Web.UI.Page
{

    ECustomer userBLL = new ECustomer();
    protected void Page_Load(object sender, EventArgs e)
    {
        string phone = Request["phone"].ToString().Trim();

        string code = WebUtility.GetRandomOnlyNum(6);

        Session["gsmcode"] = code;
        Session.Timeout = 10;

        Regex rx = new Regex(@"^[0-9]*$");
        if (rx.IsMatch(phone) && phone.Length == 11)
        {

        }
        else
        {
            Response.Write("0");
            Response.End();
            return;
        }

        try
        {
            string fuc = Request["fuc"];
            switch (fuc)
            {
                case "auth"://手机认证，要判处有没有认证过
                    {
                        string mobile = phone;//手机
                        ECustomerInfo uinfo = UserHelp.GetUser();
                        string cont = "";
                        if (uinfo == null)
                        {
                            string sql = "Tell = '" + mobile + "'";
                            int count = userBLL.GetCount(sql);

                            if (count > 0)
                            {
                                cont = "欢迎来"+SectionProxyData.GetSetValue(2)+"点餐,您已注册过，可以用您的账号密码登陆进行订餐。";//内容

                                int rs = SentGsm.sendMsg(mobile, cont);
                                Response.Write(rs);
                              
                            }
                            else
                            {

                                //通知用户
                                WebUtility.FixsetCookie("gsmcode", code, 1);
                                cont = "欢迎来"+SectionProxyData.GetSetValue(2)+"点餐,您的6位数字验证码是：" + code + ";";//内容
                                int rs = SentGsm.sendMsg(mobile, cont);
                                Response.Write(rs);
                            }

                        }
                        else
                        {
                           // cont = "欢迎来吃饭网点餐,您的4位数字验证码是：" + code + ";您也可以用您的手机号，密码：" + code + "来登陆吃饭网查询您的订单信息。";//内容

                        }

                        break;
                    }

                case "authcode"://手机提交订单验证
                    {
                        int rs = 0;
                        string mobile = phone;//手机
                        string s = checkphone(mobile);
                        if (s == "1")
                        {
                            rs = -3;
                        }
                        else
                        {
                            WebUtility.FixsetCookie("gsmcode", code, 1);
                            string cont = "欢迎来到" + SectionProxyData.GetSetValue(2) + ",您的验证码是：" + code;//内容
                            rs = SentGsm.sendMsg(mobile, cont);
                        }
                        Response.Write(rs);
                        break;
                    }
                case "deliverreg"://配送员注册 
                    {
                        int rs = 0;
                        string mobile = phone;//手机
                        if (new Deliver().GetCount("Phone = '" + phone + "'") > 0)
                        {
                            rs = -3;
                        }
                        else
                        {
                            WebUtility.FixsetCookie("gsmcode", code, 1);
                            rs = Hangjing.WebCommon.SendMsg.deliver_SendValidCode(phone, code);
                        }
                        Response.Write(rs);
                        break;
                    }

                    

            }



        }
        catch
        {
            Response.Write("0");
        }
        Response.End();
    }

    protected string checkphone(string phone)
    {
        string rs = "0";
        string sql = string.Empty;
        sql = " Tell = '" + phone + "'";
        int count = new ECustomer().GetCount(sql);
        if (count > 0)
        {
            rs = "1";//存在
        }
        return rs;
    }
}
